/// <reference path="./Vector2.ts" />
namespace apeng {


    export interface ICircle {
        x: number
        y: number
        radius: number
    }
    type TCircleSet = ICircle | number

    export class Circle implements ICircle {
        public static TEMP = new Circle()

        public static set(self: Circle, x?: TCircleSet, y?: number, radius?: number): Circle {
            if (x && typeof x === "object") {
                self.x = x.x
                self.y = x.y
                self.radius = x.radius || 0
            } else {
                self.x = (x || 0) as number
                self.y = y || 0
                self.radius = radius || 0
            }
            return self
        }

        public static create(x?: TCircleSet, y?: number, radius?: number): Circle {
            return new Circle(x, y, radius)
        }

        public static clone(orgin: ICircle) {
            return new Circle(orgin)
        }

        /**判断当前园是否包含指定的点 */
        public static contains(self: ICircle, point: IVector2) {
            let distance = Vector2.distance(self, point)
            return distance < self.radius
        }

        /**判断当前园是否与指定矩形相交 */
        public static intersects(self: ICircle, other: ICircle): boolean {
            let distance = Vector2.distance(self, other)
            return distance < self.radius
        }

        /**距中心点 */
        public static mul(out: Circle, value: Circle, mul: number): Circle {
            out.set(value)
            out.radius *= mul
            return out
        }

        public x: number = 0
        public y: number = 0
        public radius: number = 0

        private _position = new Vector2()

        public get position(): Vector2 {
            this._position.set(this.x, this.y)
            return this._position
        }

        public set position(value: Vector2) {
            this.x = value.x
            this.y = value.y
        }

        constructor(x?: TCircleSet, y?: number, radius?: number) {
            this.set(x, y, radius)
        }

        public set(x?: TCircleSet, y?: number, radius?: number) { Circle.set(this, x, y, radius); return this }
        public intersects(other: ICircle) { return Circle.intersects(this, other) }
        public contains(point: IVector2) { return Circle.contains(this, point) }
        public clone() { return Circle.clone(this) }
        public mul(out: Circle, mul: number) { return Circle.mul(out, this, mul) }
        public mulSelf(mul: number) { return Circle.mul(this, this, mul) }

    }
}